package com.tsingtao.springboot2.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.sql.SqlInjectionUtils;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tsingtao.springboot2.dto.User;
import com.tsingtao.springboot2.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;


    @GetMapping("/select-all")
    public List<User> selectAll() {
        return userService.list();
    }

    @GetMapping("/select-by-wrapper")
    public List<User> selectByWrapper() {
        List<User> list;

        // QueryWrapper
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq(User.Fields.name, "Tom");
//        list = userService.list(userQueryWrapper);

        // LambdaQueryWrapper
        LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userLambdaQueryWrapper.eq(User::getName, "Tom");
//        list = userService.list(userLambdaQueryWrapper);

        // QueryChainWrapper
        QueryChainWrapper<User> queryChainWrapper = userService
                .query()
                .eq(User.Fields.name, "Tom");
//        list = queryChainWrapper.list();

        // LambdaQueryChainWrapper
        LambdaQueryChainWrapper<User> userLambdaQueryChainWrapper = userService.lambdaQuery()
                .eq(User::getName, "Tom");
        list = userLambdaQueryChainWrapper.list();

        return list;
    }

    @GetMapping("/test-query-wrapper")
    public List<User> testQueryWrapper() {
        return userService.testQueryWrapper();
    }

    @PutMapping("/test-update-wrapper")
    public List<User> testUpdateWrapper() {
        return userService.testUpdateWrapper();
    }

    @GetMapping("/test-custom-select-all")
    public List<User> customSelectAll() {
        return userService.customSelectAll();
    }

    @PostMapping("/test-save")
    public User testSave(@RequestBody User user) {
        return userService.testSave(user);
    }

    @PostMapping("/test-batch-save")
    public List<User> testBatchSave(@RequestBody List<User> userList) {
        return userService.testBatchSave(userList);
    }

    @DeleteMapping("/test-delete-by-id")
    public boolean testDeleteById(@RequestParam Long id) {
        return userService.removeById(id);
    }

    @PutMapping("/test-update-by-id")
    public User testUpdateById(@RequestBody User user) {
        log.info("更新参数：{}", user);
        boolean update = userService.updateById(user);
        log.info("更新是否成功：{}", update);
        return user;
    }

    @GetMapping("/test-page")
    public IPage<User> testPage(Page<User> page, @RequestBody User user) {
        return userService.testPage(page, user);
    }

    @GetMapping("/test-inject")
    public List<User> testInject(@RequestParam String sql) {
        boolean check = SqlInjectionUtils.check(sql);
        if(check) {
            throw new RuntimeException("SQL注入");
        }
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.apply(sql);
        List<User> list = userService.list(queryWrapper);
        return list;
    }

}
